var Week = (function() {
	/*定义addClass方法*/
	function addClass(obj, cls){
	  obj.className = obj.className + " " + cls;
	}
	/*定义removeClass方法*/
	function removeClass(obj, cls){
	  obj.className = obj.className.replace(' '+cls,'');
	}
	function Week(opt) {
		this.init(opt);
	}
	Week.prototype.init = function(opt) {
		this.idName = {
			eleID: opt.id,
			dayContentID: opt.id + "_dayContent",
			leftBtnContentID: opt.id + "_leftBtnContent",
			rightBtnContentID: opt.id + "_rightBtnContent",
			dayContentInnerID: opt.id + "dayContentInner"
		}
		this.callBack = opt.callBack;
		this.dateData = [];
		this.createOuterHtml(opt);
		this.resetDate(opt.dateData);
		document.getElementById(this.idName.leftBtnContentID).addEventListener("click", this.setSwitch.bind(this));
		document.getElementById(this.idName.rightBtnContentID).addEventListener("click", this.setSwitch.bind(this));
	}
	Week.prototype.createOuterHtml = function(opt) {
		var dayContent = '<div id="' + this.idName.dayContentID + '" class="day-content"></div>';
		var leftBtnContent = '<div id="' + this.idName.leftBtnContentID + '" class="btn-ontent left"></div>';
		var rightBtnContent = '<div id="' + this.idName.rightBtnContentID + '" class="btn-ontent right"></div>';
		document.getElementById(this.idName.eleID).innerHTML = dayContent + leftBtnContent + rightBtnContent;
	}
	Week.prototype.createInnerHtml = function() {
		var mouthDayCount = this.dateData.length;
		var html = '<div id="' + this.idName.dayContentInnerID + '" class="inner">';
		for(var i = 0; i < mouthDayCount; i++) {
			html += '<div class="day" data-date="' + this.dateData[i].year + "/" + this.dateData[i].mouth + '/' + this.dateData[i].mouthDay + '/' + this.dateData[i].localWeekDay + '">' +
				'<span>' + this.dateData[i].localWeekDay + '</span>' +
				'<span>' + this.dateData[i].mouth + '.' + this.dateData[i].mouthDay + '</span>' +
				'</div>';
		}
		document.getElementById(this.idName.dayContentID).innerHTML = html+"</div>";
		this.setSize(mouthDayCount);
	}
	Week.prototype.setSize = function(mouthDayCount) {
		this.stepWidth = document.getElementById(this.idName.dayContentID).offsetWidth;
		this.moveElement = document.getElementById(this.idName.dayContentInnerID);
		this.moveElement.style.width = (this.stepWidth / 4) * mouthDayCount + "px";
		this.maxWidth = this.moveElement.offsetWidth;
		var allDayEleArray = this.moveElement.querySelectorAll(".day");
		var isPhone = ("ontouchstart" in window)? true : false;
		var dayWidth = this.stepWidth / 4;
		for(var ii = 0; ii < allDayEleArray.length; ii++) {
			allDayEleArray[ii].style.width = dayWidth + "px";
			if(isPhone){
				allDayEleArray[ii].addEventListener("touchstart", this.getFormatDate.bind(this), false);
			}else{
				allDayEleArray[ii].addEventListener("click", this.getFormatDate.bind(this), false);
			}
		}
		
		this.position = -(this.stepWidth * this.stepCount);
		console.log(this.stepWidth)
		console.log(this.stepCount)
		console.log(this.position)
		this.getFormatDate(allDayEleArray[this.activeDayCount-1],true);
		this.moveElement.style.left = this.position;
		this.moveElement.addEventListener('transitionend', this.checkTransition.bind(this), false);
	}
	Week.prototype.setSwitch = function(event) {
		if(this.transitionEnd) {
			if(event.target.id == this.idName.leftBtnContentID) {
				if(this.position >= 0) return;
				this.position += this.stepWidth;
				this.moveElement.style.left = this.position;
			} else {
				if(this.position <= -(this.maxWidth - this.stepWidth)) return;
				this.position -= this.stepWidth;
				this.moveElement.style.left = this.position;
			}
			this.transitionEnd = false;
		}
	}
	Week.prototype.checkTransition = function() {
		this.transitionEnd = true;
	}
	Week.prototype.resetDate = function(dateData) {
		this.resetProperty();
		var year,/*某年*/
			mouth,/*某月*/
			mouthDay,/*某月的某号*/
			thisMouthDay = NaN,/*今天的号数*/
			mouthDayCount,/*某月天数*/
			localWeekDay,/*某月的某号对应的星期几*/
			localWeekDayArray = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
			if(dateData){
				year = dateData.substr(0, 4);
				mouth = dateData.substr(5);
				mouthDayCount = new Date(year, mouth, 0).getDate();
			}else{
				year = new Date().getFullYear();
				mouth = new Date().getMonth()+1;
				mouth = mouth > 9 ? mouth : (0 + '' + mouth);
				mouthDayCount = new Date(year, mouth, 0).getDate();
				thisMouthDay = new Date().getDate();
			}
			for(var i = 1; i <= mouthDayCount; i++) {
				mouthDay = i > 9 ? i : (0 + '' + i);
				if(mouthDay == thisMouthDay){
					this.stepCount = i==1 ? 0 : Math.floor((i-1)/4);
					this.activeDayCount = i;
				}
				localWeekDay = localWeekDayArray[new Date(year + "/" + mouth + "/" + mouthDay).getDay()];
				this.dateData.push({
					year:year,
					mouth:mouth,
					mouthDay:mouthDay.toString(),
					localWeekDay:localWeekDay
				});
			}
		this.createInnerHtml();
	}
	Week.prototype.resetProperty = function() {
		this.dateData.length = 0;/*日期数据    包含 某年、某月、某号、某月的某号对应的星期几*/
		this.lastEle = null;/*上一次点击的day元素*/
		this.position = 0;/*移动元素的位置*/
		this.transitionEnd = true;/*判断移动元素动画是否执行完毕*/
		this.stepCount = 0;/*用于计算今天或者传入日期参数对应的月份的1号所在的位置,以便进行定位*/
		this.activeDayCount = 1;/*默认输出今天对应的日期,否则输出传入日期参数对应的月份的1号对应的日期*/
	}
	Week.prototype.getFormatDate = function(event,selfTrigger) {
		var thisEle,formatDate;
		if(selfTrigger){
			thisEle = event;
			formatDate = event.getAttribute("data-date");
		}else if(event.srcElement.dataset.date) {
			thisEle = event.srcElement;
			formatDate = event.srcElement.dataset.date;
		} else {
			thisEle = event.srcElement.parentNode;
			formatDate = event.srcElement.parentNode.getAttribute("data-date");
		}
		addClass(thisEle,"active");
		if(this.lastEle){
			removeClass(this.lastEle,"active");
		}
		this.lastEle = thisEle;
		this.callBack(formatDate);
	}
	return Week;
})();